package labox.innovation.gameserver.model.zone;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javolution.util.FastList;
import javolution.util.FastMap;
import labox.innovation.gameserver.model.L2Object;
import labox.innovation.gameserver.model.actor.L2Character;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.model.quest.Quest;
import labox.innovation.gameserver.network.serverpackets.L2GameServerPacket;

/**
 * Abstract base class for any zone type
 * Handles basic operations
 *
 * @author  durgus
 */
public abstract class L2ZoneType {
	
	protected static final Logger _log = LoggerFactory.getLogger(L2ZoneType.class.getName());

	private final int _id;

	protected List<L2ZoneForm> _zone;

	protected FastMap<Long, L2Character> _characterList;

	protected FastMap<Integer, Integer> _zones;

	/** Parameters to affect specific characters */
	private boolean _checkAffected;

	private int _minLvl;

	private int _maxLvl;

	private int[] _class;

	private char _classType;

	private Map<Quest.QuestEventType, FastList<Quest>> _questEvents;

	protected L2ZoneType(int id) {
		_id = id;
		_characterList = new FastMap<Long, L2Character>();
		_zones = new FastMap<Integer, Integer>().shared();

		_checkAffected = false;

		_minLvl = 0;
		_maxLvl = 0xFF;

		_classType = 0;

		_class = null;
	}

	/**
	 * @return Returns the id.
	 */
	public int getId() {
		return _id;
	}

	/**
	 * 设置地带的影响的参数
	 * @param type
	 * @param value
	 */
	public void setParameter(String name, String value) {
		_checkAffected = true;

		//影响的最小等级
		if (name.equals("affectedLvlMin")) {
			_minLvl = Integer.parseInt(value);
		}
		//影响的最大等级
		else if (name.equals("affectedLvlMax")) {
			_maxLvl = Integer.parseInt(value);
		}
		//影响的职业
		else if (name.equals("affectedClassId")) {
			// Create a new array holding the affected classIds
			if (_class == null) {
				_class = new int[1];
				_class[0] = Integer.parseInt(value);
			} else {
				int[] temp = new int[_class.length + 1];

				int i = 0;
				for (; i < _class.length; i++)
					temp[i] = _class[i];

				temp[i] = Integer.parseInt(value);

				_class = temp;
			}
		}
		// 影响的职业类型
		else if (name.equals("affectedClassType")) {
			if (value.equals("Fighter")) {
				_classType = 1;
			} else {
				_classType = 2;
			}
		}
	}

	/**
	 * 检查一个L2Character是否被此区域影响
	 * @param character
	 * @return
	 */
	private boolean isAffected(L2Character character) {
		// Check lvl
		if (character.getLevel() < _minLvl || character.getLevel() > _maxLvl)
			return false;

		if (character instanceof FuzePcInstance) {
			// Check class type
			if (_classType != 0) {
				if (((FuzePcInstance) character).isMageClass()) {
					if (_classType == 1)
						return false;
				} else if (_classType == 2)
					return false;
			}

			// Check class
			if (_class != null) {
				boolean ok = false;

				for (int i = 0; i < _class.length; i++) {
					if (((FuzePcInstance) character).getClassId().ordinal() == _class[i]) {
						ok = true;
						break;
					}
				}

				if (!ok)
					return false;
			}
		}
		return true;
	}

	/**
	 * 设置这个地带类型的形状
	 * @param zone
	 */
	public void setZone(L2ZoneForm zone) {
		getZones().add(zone);
	}

	/**
	 * 获取这个地带类型的第一个形状
	 * @param zone
	 * @return
	 */
	public L2ZoneForm getZone() {
		for (L2ZoneForm zone : getZones()) {
			return zone;
		}
		return null;
	}

	/**
	 * 获取这个地带类型的所有形状
	 * @param zone
	 * @return
	 */
	public final List<L2ZoneForm> getZones() {
		if (_zone == null)
			_zone = new FastList<L2ZoneForm>();
		return _zone;
	}

	/**
	 * 检测某个点是否在这个地带中
	 * @param x
	 * @param y
	 */
	public boolean isInsideZone(int x, int y) {
		for (L2ZoneForm zone : getZones()) {
			if (zone.isInsideZone(x, y))
				return true;
		}
		return false;
	}

	/**
	 * 检测一个对象是不是在这个地带内
	 *
	 * @param object
	 */
	public boolean isInsideZone(L2Object object) {
		return isInsideZone(object.getX(), object.getY());
	}

	/**
	 * 获得某个点到第一个形状的最短距离
	 * @param x
	 * @param y
	 * @return
	 */
	public double getDistanceToZone(int x, int y) {
		return getZone().getDistanceToZone(x, y);
	}

	/**
	 * 获得某个对象到第一个形状的最短距离
	 * @param object
	 * @return
	 */
	public double getDistanceToZone(L2Object object) {
		return getZone().getDistanceToZone(object.getX(), object.getY());
	}

	/**
	 * 检测L2Character与此Zone交互逻辑
	 * @param character
	 */
	public void revalidateInZone(L2Character character) {
		//如果一个L2Character不被此地带影响则返回
		if (_checkAffected) {
			if (!isAffected(character))
				return;
		}

		// 如果角色在此地带中
		if (isInsideZone(character.getX(), character.getY())) {
			// 此角色还没有被记录的话，执行进入
			if (!_characterList.containsKey(character.getObjectId())) {
				FastList<Quest> quests = getQuestByEvent(Quest.QuestEventType.ON_ENTER_ZONE);
				if (quests != null) {
					for (Quest quest : quests) {
						quest.notifyEnterZone(character, this);
					}
				}
				_characterList.put(character.getObjectId(), character);
				onEnter(character);
			}
		} else {
			removeCharacter(character);
		}
	}

	/**
	 * 强行从此地带中删除一个L2Character
	 * Should use during teleport / logoff
	 * @param character
	 */
	public void removeCharacter(L2Character character) {
		if (_characterList.containsKey(character.getObjectId())) {//如果角色被记录的话,执行离开
			FastList<Quest> quests = getQuestByEvent(Quest.QuestEventType.ON_EXIT_ZONE);
			if (quests != null) {
				for (Quest quest : quests) {
					quest.notifyExitZone(character, this);
				}
			}
			_characterList.remove(character.getObjectId());
			onExit(character);
		}
	}

	/**
	 * 指定的角色是否存在于此地带
	 * @param character
	 * @return
	 */
	public boolean isCharacterInZone(L2Character character) {
		return _characterList.containsKey(character.getObjectId());
	}
	
	protected abstract void onEnter(L2Character character);

	protected abstract void onExit(L2Character character);

	public abstract void onDieInside(L2Character character);

	public abstract void onReviveInside(L2Character character);

	public FastMap<Long, L2Character> getCharactersInside() {
		return _characterList;
	}

	/**
	 * 根据任务的事件类型，加入对应的任务，一个类型的事件对应一个这类事件的队列
	 * @param EventType
	 * @param q
	 */
	public void addQuestEvent(Quest.QuestEventType EventType, Quest q) {
		if (_questEvents == null)
			_questEvents = new FastMap<Quest.QuestEventType, FastList<Quest>>();
		FastList<Quest> questByEvents = _questEvents.get(EventType);
		if (questByEvents == null){
			questByEvents = new FastList<Quest>();
			_questEvents.put(EventType, questByEvents);
		}
		if (!questByEvents.contains(q))
			questByEvents.add(q);
		
	}

	/**
	 * 根据任务事件类型，获得这一类事件的所有对象
	 * @param EventType
	 * @return
	 */
	public FastList<Quest> getQuestByEvent(Quest.QuestEventType EventType) {
		if (_questEvents == null)
			return null;
		return _questEvents.get(EventType);
	}

	/**
	 * 对在这个地带内的所有玩家广播一下包
	 * @param packet
	 */
	public void broadcastPacket(L2GameServerPacket packet) {
		if (_characterList.isEmpty())
			return;

		for (L2Character character : _characterList.values()) {
			character.sendPacket(packet);
		}
	}
}
